package com.kykj.kykjyzw.controller;

import com.kykj.kykjyzw.entity.AddressEntity;
import com.kykj.kykjyzw.entity.ResultEntity;
import com.kykj.kykjyzw.service.impl.AddressServiceImpl;
import com.kykj.kykjyzw.vo.AddressVo;
import com.sun.source.tree.TryTree;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping("v2/address/")
@RestController
@CrossOrigin
public class AddressController {
    @Resource
    AddressServiceImpl addressService;

    /**
     * 获取地址列表
     * @param uid 用户id
     * @param limit 每页行数
     * @param page 页码
     * @return list
     */
    @GetMapping("address")
    private ResultEntity getAddressList(
            @RequestParam(value = "uid",defaultValue = "-1") int uid,
            @RequestParam("limit") int limit,
            @RequestParam("page") int page
    ){
        ResultEntity resultEntity = new ResultEntity();
        try{
            int start=(page-1)*limit;
            List<AddressEntity> list=addressService.getAddressListByUser(uid, start, limit);
            if (uid==-1){
                resultEntity.setCount(addressService.getAddressCount());
            }else {
                resultEntity.setCount(addressService.getAddressListByUser(uid, start, limit));
            }
            resultEntity.setData(list);
            resultEntity.setCode(0);
            resultEntity.setMsg("获取成功");
        }catch (Exception e){
            e.printStackTrace();
            resultEntity.setCode(-99999);
            resultEntity.setMsg("获取数据失败");
        }
        return resultEntity;
    }

    /**
     * 添加地址
     * @param addressVo 地址对象
     * @return int：影响的行数
     */
    @PostMapping("address")
    private ResultEntity addAddress(@RequestBody AddressVo addressVo){
        ResultEntity resultEntity = new ResultEntity();
        try{
            if (addressService.addAddress(addressVo)>0){
                resultEntity.setCode(0);
                resultEntity.setMsg("数据添加成功");
            }else {
                resultEntity.setCode(-1);
                resultEntity.setMsg("数据添加失败");
            }
        }catch (Exception e){
            e.printStackTrace();
            resultEntity.setCode(-99999);
            resultEntity.setMsg("获取数据失败");
        }
        return resultEntity;
    }

    /**
     * 修改地址
     * @param addressVo 地址对象
     * @return int;影响的行数
     */
    @PutMapping("address")
    private ResultEntity upAddress(@RequestBody AddressVo addressVo){
        ResultEntity resultEntity = new ResultEntity();
        try{
            if (addressService.changeAddress(addressVo)>0){
                resultEntity.setCode(0);
                resultEntity.setMsg("数据修改成功");
            }else {
                resultEntity.setCode(1);
                resultEntity.setMsg("数据修改失败");
            }
        }catch (Exception e){
            e.printStackTrace();
            resultEntity.setCode(-99999);
            resultEntity.setMsg("获取数据失败");
        }
        return resultEntity;
    }

    /**
     * 删除地址
     * @param addressid 地址id
     * @return int;影响的行数
     */
    @DeleteMapping("address/{addressid}")
    private ResultEntity delAddress(@PathVariable("addressid") int addressid){
        ResultEntity resultEntity = new ResultEntity();
        try{
            if (addressService.delAddress(addressid)>0){
                resultEntity.setCode(0);
                resultEntity.setMsg("数据删除成功");
            }else {
                resultEntity.setCode(1);
                resultEntity.setMsg("数据删除失败");
            }
        }catch (Exception e){
            e.printStackTrace();
            resultEntity.setCode(-99999);
            resultEntity.setMsg("获取数据失败");
        }
        return resultEntity;
    }
}
